//
//  TravelDetailViewController.m
//  VTravel
//
//  Created by lanouhn on 16/8/5.
//  Copyright © 2016年 xiaoYu. All rights reserved.
//

#import "TravelDetailViewController.h"
#import "TravelListTableViewCell.h"
#import "TravelListModel.h"
#import "RootTableHeaderView.h"
#import "RootScrollViewModel.h"
/** 点击cell进入的视图控制器 */
#import "TravelDeatilListDVC.h"
/** 点击按钮进入的视图控制器 */
#import "NearByCellClickDetailVC.h"

#import "CityViewController.h"

@interface TravelDetailViewController ()<UITableViewDelegate, UITableViewDataSource, UISearchControllerDelegate, UISearchResultsUpdating>

/** 底部的表视图 */
@property (strong, nonatomic) IBOutlet UITableView *rootTableView;

/** 轮播图对象 */
@property (nonatomic, strong) SDCycleScrollView *sdcScrollView;
/** 轮播图数据源数组 */
@property (nonatomic, strong) NSMutableArray *TravelSVDataArray;
/** 旅游列表数据源数组 */
@property (nonatomic, strong) NSMutableArray *travelListDataArray;

@property (nonatomic, strong) NSArray *array1;
@property (nonatomic, strong) NSArray *array2;

/** 表头上面的搜索框 */
@property (nonatomic, strong) UISearchController *searchVC;
@property (nonatomic, strong) UIButton *cityButton;


@end

@implementation TravelDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.travelListDataArray = [NSMutableArray array];
    self.TravelSVDataArray = [NSMutableArray array];
    self.array1 = @[@"微旅推荐",@"水上漂流",@"海岛假期"];
    self.array2 = @[@"性价之王", @"漂流哪好玩", @"优选全球海岛"];
    
    self.title = @"旅游";
    // 设置cell的自适应高度
    self.rootTableView.estimatedRowHeight = 250;
    self.rootTableView.rowHeight = UITableViewAutomaticDimension;
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 请求数据
        [self requestData];
    });
    // 创建表头视图
    [self creatTableHeaderView];
}

#pragma mark - 数据请求方法
- (void)requestData{
    [NetWorkManager requestForGETWithURL:KTravelRootURL paramater:@{@"city_id":city_id} success:^(id responseObject) {
        
        // 轮播图图片数据
        for (NSDictionary *dic in responseObject[@"data"][@"index_ads"]) {
            RootScrollViewModel *model = [[RootScrollViewModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            model.src = ImageURL(dic[@"src"]);
            [self.TravelSVDataArray addObject:model.src];
        }
        
        // 旅游列表cell数据
        for (NSDictionary *dic in responseObject[@"data"][@"jp_products"]) {
            TravelListModel *model = [[TravelListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            model.travel_tag = [dic[@"travel_tag"] componentsSeparatedByString:@","];
            
            [self.travelListDataArray addObject:model];
        }
        // 刷新数据
        dispatch_async(dispatch_get_main_queue(), ^{
            // 创建表头视图
            [self creatTableHeaderView];
            self.sdcScrollView.imageURLStringsGroup = self.TravelSVDataArray;
            [self.rootTableView reloadData];
        });
        
    } fail:^(NSError *error) {
        kMyLog(@"%@", error);
    }];
}
#pragma mark - 创建表头视图方法
- (void)creatTableHeaderView{
    NSArray *tempArray = @[@"周边游", @"国内游", @"港澳台", @"出境游", @"境外参团"];
    RootTableHeaderView *headerView = [[RootTableHeaderView alloc] initWithRootHeaderViewFrame:CGRectMake(0, 44, kScreenWidth, 320) andBtnTitleArray:tempArray];
    self.sdcScrollView = headerView.sdcycleScrollView;
    headerView.customBtnBlock = ^(NSInteger index){
        kMyLog(@"点击了%@的按钮", tempArray[index]);
        NearByCellClickDetailVC *detailVC = [[NearByCellClickDetailVC alloc] init];
        detailVC.hidesBottomBarWhenPushed = YES;
        detailVC.type = XYPushTransitionTypeFromRootTravelButtonCells;
        switch (index) {
            case 0:
                detailVC.route_type = @"-11";
                break;
            case 1:
                detailVC.route_type = @"-12";
                break;
            case 2:
                detailVC.route_type = @"-14";
                break;
            case 3:
                detailVC.route_type = @"-13";
                break;
            case 4:
                detailVC.route_type = @"-15";
                break;
            default:
                break;
        }
        [self.navigationController pushViewController:detailVC animated:YES];
    };
    headerView.scrollViewBlock = ^(NSInteger index){
        kMyLog(@"点击了下标为%ld的轮播图", index);
    };
    [self creatSearchController];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 364)];
    [view addSubview:headerView];
    
    [view addSubview:self.searchVC.searchBar];
    
    self.rootTableView.tableHeaderView = view;
}
#pragma mark - tableView的代理方法
#pragma mark - 返回分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
#pragma mark - 返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return section == 0 ?  1 : self.travelListDataArray.count + 1;
}
#pragma mark - 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 重用
    static NSString *identifier = @"CELL";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell  == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    if (indexPath.section == 0) {
        NSArray *array = @[@"travle_gift_image", @"travle_mountain_image", @"travle_holiday_image"];

        for (int i = 0; i < 3; i ++) {
            
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
            button.frame = CGRectMake(i * kScreenWidth / 3, 5, kScreenWidth / 3, 120);
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(25, 50, kScreenWidth / 3 - 55, 50)];
            imageView.image = [UIImage imageNamed:array[i]];
            [button addSubview:imageView];

            button.tag = 500 + i;
            
            if (i != 2) {
                UIView *view = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth / 3 - 2, 0, 2, 120)];
                view.backgroundColor = [UIColor lightGrayColor];
                [button addSubview:view];
            }
            
            UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth / 6 - 50, 5, 100, 15)];
            label1.text = self.array1[i];
            label1.textAlignment = NSTextAlignmentCenter;
            label1.textColor = kRandomColor;
            label1.font = [UIFont systemFontOfSize:15];
            [button addSubview:label1];

            UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth / 6 - 50, 25, 100, 15)];
            label2.text = self.array2[i];
            label2.textAlignment = NSTextAlignmentCenter;
            label2.textColor = [UIColor grayColor];
            label2.font = [UIFont systemFontOfSize:13];
            [button addSubview:label2];
            
            [button addTarget:self action:@selector(handlePictureAction:) forControlEvents:(UIControlEventTouchUpInside)];
            
            [cell addSubview:button];
        }
    }else{
        if (indexPath.row == 0) {
            cell.textLabel.text = @"精品路线";
        }else{
            static NSString *indetifier = @"TravelListTVCell";
            TravelListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:@"TravelListTableViewCell" owner:self options:nil] firstObject];
            }
            if (self.travelListDataArray.count != 0) {
                TravelListModel *model = self.travelListDataArray[indexPath.row - 1];
                cell.model = model;
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - 图片点击事件
- (void)handlePictureAction:(UIButton *)sender{
    kMyLog(@"点击了第%ld张图片", sender.tag - 500);
    NearByCellClickDetailVC *detailVC = [[NearByCellClickDetailVC alloc] init];
    detailVC.hidesBottomBarWhenPushed = YES;
    detailVC.TitleStr = self.array1[sender.tag - 500];
    detailVC.type = XYPushTransitionTypeFromRootTravelButtonCells;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TravelListModel *model = self.travelListDataArray[indexPath.row - 1];
    kMyLog(@"点击了%@", model.product_name);
    
    TravelDeatilListDVC *travelListDVC = [[TravelDeatilListDVC alloc] init];
    travelListDVC.model = self.travelListDataArray[indexPath.row - 1];
    travelListDVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:travelListDVC animated:YES];
    
    
}
#pragma mark - 区头高度的方法
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
#pragma mark - 返回cell高度的方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 120;
    }else{
        if (indexPath.row == 0) {
            return 44;
        }else{
            return -1;
        }
    }
}
#pragma mark --  配置搜索视图控制器
- (void)creatSearchController{
    CityViewController *cityVC = [[CityViewController alloc] init];
    self.searchVC = [[UISearchController alloc] initWithSearchResultsController:cityVC];
    self.searchVC.searchResultsUpdater = self;
    self.searchVC.delegate = self;
    self.searchVC.hidesNavigationBarDuringPresentation = YES;
    self.searchVC.searchBar.barTintColor = [UIColor lightGrayColor];
    
    // 配置searchBar
    // 城市按钮
    self.cityButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cityButton.frame = CGRectMake(0, 0, 90, 44);
    [self.cityButton setTitle:@"城市" forState:(UIControlStateNormal)];
    [self.cityButton setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    self.cityButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.cityButton setImage:[UIImage imageNamed:@"矩形-36"] forState:(UIControlStateNormal)];
    [self.cityButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 20)];
    [self.cityButton setImageEdgeInsets:UIEdgeInsetsMake(0, 70, 0, 0)];
    [self.cityButton addTarget:self action:@selector(handleButtonClickAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.searchVC.searchBar addSubview:self.cityButton];

    // 创建搜索输入框
    UITextField *searchTF = [[UITextField alloc] initWithFrame:CGRectMake(90, 8, kScreenWidth - 90 - 45, 28)];
    searchTF.layer.cornerRadius = 5;
    searchTF.layer.borderWidth = 1;
    searchTF.layer.borderColor = [UIColor whiteColor].CGColor;
    searchTF.borderStyle = UITextBorderStyleRoundedRect;
    searchTF.backgroundColor = [UIColor whiteColor];
    searchTF.placeholder = @"请输入目的地或关键字";
    searchTF.font = [UIFont systemFontOfSize:15];
    // 创建输入框上的图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 30, 15)];
    imageView.image = [UIImage imageNamed:@"Search"];
    imageView.contentMode = UIViewContentModeScaleAspectFit; // 设置图片的填充样式
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2, 15)];
    view.backgroundColor = [UIColor lightGrayColor];
    [imageView addSubview:view];
    
    searchTF.leftView = imageView;
    searchTF.leftViewMode = UITextFieldViewModeAlways;
    [self.searchVC.searchBar addSubview:searchTF];
    
    // 创建二维码按钮
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    searchButton.frame = CGRectMake(kScreenWidth - 45, 0, 40, 40);
    [searchButton setTitle:@"搜索" forState:(UIControlStateNormal)];
    [searchButton addTarget:self action:@selector(ClickSearchButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.searchVC.searchBar addSubview:searchButton];
}
#pragma mark - 城市按钮点击事件
- (void)handleButtonClickAction{
    kMyLog(@"点击了城市按钮");
}
#pragma mark - 搜索按钮点击事件
- (void)ClickSearchButtonAction{
    kMyLog(@"搜一搜");
}
#pragma mark - UISearchResultsUpdating 协议
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    kMyLog(@"搜搜搜");
}
@end
